<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>消息队列重复消费解决方案</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        .highlight-box {
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
            border-left: 4px solid #667eea;
            padding: 1.5rem;
            margin: 1.5rem 0;
            border-radius: 0.5rem;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white">
        <div class="container mx-auto px-6 py-20">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    消息队列重复消费
                    <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">完美解决方案</span>
                </h1>
                <p class="text-xl md:text-2xl font-light opacity-90 leading-relaxed">
                    深入探索五大核心策略，构建高可靠性的分布式消息系统
                </p>
                <div class="mt-10 flex justify-center space-x-8">
                    <div class="text-center">
                        <i class="fas fa-shield-alt text-4xl mb-2"></i>
                        <p class="text-sm">数据一致性</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-sync-alt text-4xl mb-2"></i>
                        <p class="text-sm">幂等性保障</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-check-double text-4xl mb-2"></i>
                        <p class="text-sm">去重机制</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <div class="max-w-6xl mx-auto">
            
            <!-- Introduction -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap">在</span>消息队列（MQ）系统中，重复消费是指消费者由于某种原因多次处理相同的消息，这通常会导致数据不一致、业务逻辑错误或资源浪费。为了确保系统的正确性和高效性，解决重复消费是非常重要的。
                </p>
            </div>

            <!-- Architecture Diagram -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
                <h2 class="text-3xl font-bold mb-6 text-center text-gray-800">解决方案架构图</h2>
                <div class="mermaid">
                    graph TB
                        A[消息生产者] -->|发送消息| B[消息队列]
                        B -->|消费消息| C[消费者]
                        C --> D{重复检测}
                        D -->|已处理| E[跳过消息]
                        D -->|未处理| F[处理消息]
                        F --> G[幂等性操作]
                        G --> H[确认消息]
                        H --> I[记录处理状态]
                        
                        style A fill:#667eea,stroke:#fff,color:#fff
                        style B fill:#764ba2,stroke:#fff,color:#fff
                        style C fill:#667eea,stroke:#fff,color:#fff
                        style G fill:#48bb78,stroke:#fff,color:#fff
                </div>
            </div>

            <!-- Solution 1: Message Deduplication -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
                <h2 class="text-3xl font-bold mb-8 section-title text-gray-800">
                    <i class="fas fa-fingerprint feature-icon mr-3"></i>消息去重
                </h2>
                
                <div class="grid md:grid-cols-2 gap-8">
                    <div class="space-y-6">
                        <div>
                            <h3 class="text-xl font-semibold mb-3 text-gray-700">
                                <i class="fas fa-key text-purple-600 mr-2"></i>消息唯一标识符
                            </h3>
                            <div class="highlight-box">
                                <p class="text-gray-600 mb-3"><strong>原理：</strong>每个消息在发送到队列时都会附加一个唯一的标识符（如 UUID 或业务相关的唯一 ID）。消费者在处理消息时，首先检查该标识符是否已经处理过，如果是，则跳过该消息。</p>
                                <p class="text-gray-600 mb-3"><strong>实现：</strong>可以使用 Redis、数据库等存储介质来记录已处理的消息标识符。存储系统需要具有高效的查找和过期机制。</p>
                                <div class="grid grid-cols-2 gap-4 mt-4">
                                    <div class="bg-green-50 p-3 rounded">
                                        <p class="text-sm text-green-700"><i class="fas fa-check-circle mr-1"></i><strong>优点：</strong>简单有效，适用于大多数场景</p>
                                    </div>
                                    <div class="bg-red-50 p-3 rounded">
                                        <p class="text-sm text-red-700"><i class="fas fa-times-circle mr-1"></i><strong>缺点：</strong>需要额外的存储和查询开销</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="space-y-6">
                        <div>
                            <h3 class="text-xl font-semibold mb-3 text-gray-700">
                                <i class="fas fa-server text-purple-600 mr-2"></i>消息去重服务
                            </h3>
                            <div class="highlight-box">
                                <p class="text-gray-600 mb-3"><strong>原理：</strong>部署专门的去重服务来管理和检查消息的唯一性。这些服务负责接收消息、检查重复、并将唯一消息交给消费者。</p>
                                <p class="text-gray-600 mb-3"><strong>实现：</strong>去重服务可以基于缓存（如 Redis）或者数据库（如 MySQL）。服务需要处理高并发访问和数据一致性问题。</p>
                                <div class="grid grid-cols-2 gap-4 mt-4">
                                    <div class="bg-green-50 p-3 rounded">
                                        <p class="text-sm text-green-700"><i class="fas fa-check-circle mr-1"></i><strong>优点：</strong>集中化管理，容易维护</p>
                                    </div>
                                    <div class="bg-red-50 p-3 rounded">
                                        <p class="text-sm text-red-700"><i class="fas fa-times-circle mr-1"></i><strong>缺点：</strong>额外的系统组件和维护成本</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Solution 2: Message Idempotency -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
                <h2 class="text-3xl font-bold mb-8 section-title text-gray-800">
                    <i class="fas fa-infinity feature-icon mr-3"></i>消息幂等性
                </h2>
                
                <div class="grid md:grid-cols-2 gap-8">
                    <div class="space-y-6">
                        <div>
                            <h3 class="text-xl font-semibold mb-3 text-gray-700">
                                <i class="fas fa-code text-purple-600 mr-2"></i>幂等操作设计
                            </h3>
                            <div class="highlight-box">
                                <p class="text-gray-600 mb-3"><strong>原理：</strong>确保消费者的处理操作是幂等的，即重复执行相同的操作不会影响最终结果。通过设计幂等接口或处理逻辑来实现。</p>
                                <p class="text-gray-600 mb-3"><strong>实现：</strong>在业务逻辑层面，例如对数据库操作使用唯一约束、使用幂等的 REST API 设计等。</p>
                                <div class="grid grid-cols-2 gap-4 mt-4">
                                    <div class="bg-green-50 p-3 rounded">
                                        <p class="text-sm text-green-700"><i class="fas fa-check-circle mr-1"></i><strong>优点：</strong>无需额外的去重机制</p>
                                    </div>
                                    <div class="bg-red-50 p-3 rounded">
                                        <p class="text-sm text-red-700"><i class="fas fa-times-circle mr-1"></i><strong>缺点：</strong>需要重新设计业务逻辑</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="space-y-6">
                        <div>
                            <h3 class="text-xl font-semibold mb-3 text-gray-700">
                                <i class="fas fa-exchange-alt text-purple-600 mr-2"></i>事务机制
                            </h3>
                            <div class="highlight-box">
                                <p class="text-gray-600 mb-3"><strong>原理：</strong>将消息处理过程和数据存储操作放在同一个事务中，确保操作的原子性。即使发生重复消费，也不会造成数据不一致。</p>
                                <p class="text-gray-600 mb-3"><strong>实现：</strong>支持事务的消息队列（如 Kafka）、数据库事务管理等。</p>
                                <div class="grid grid-cols-2 gap-4 mt-4">
                                    <div class="bg-green-50 p-3 rounded">
                                        <p class="text-sm text-green-700"><i class="fas fa-check-circle mr-1"></i><strong>优点：</strong>提供数据一致性保障</p>
                                    </div>
                                    <div class="bg-red-50 p-3 rounded">
                                        <p class="text-sm text-red-700"><i class="fas fa-times-circle mr-1"></i><strong>缺点：</strong>引入额外的复杂性和性能开销</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Solution 3: Confirmation and Retry -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
                <h2 class="text-3xl font-bold mb-8 section-title text-gray-800">
                    <i class="fas fa-redo feature-icon mr-3"></i>消息确认与重试机制
                </h2>
                
                <div class="grid md:grid-cols-2 gap-8">
                    <div class="space-y-6">
                        <div>
                            <h3 class="text-xl font-semibold mb-